﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace BLL
{
    public class FinaceTypeBll
    {

        public List<FinaceType> SelectFinaceTypes(int memberID)
        {
            var results = from a in DB.PersonalFinanceDB.FinaceType  where a.MemberID == memberID ||  a.MemberID == -1  select a;

            List<FinaceType> lstFinaceType = results.ToList();

            return lstFinaceType;
        }

        public FinaceType SelectSingleFinaceType(int typeID)
        {
            FinaceType finaceType = DB.PersonalFinanceDB.FinaceType.Single(a => a.TypeID == typeID);
                
            return finaceType;
        }

        public bool IsPayOutFinaceType(int typeID)
        {
            FinaceType finaceType = DB.PersonalFinanceDB.FinaceType.Single(a => a.TypeID == typeID);

            return finaceType.IDLink.StartsWith(FinaceTypeIDLink.PayOut);
        }

        public void AddFinaceType(FinaceType model)
        {
            int typeID = (from a in DB.PersonalFinanceDB.FinaceType select a.TypeID).Max<int>() + 1;
            string parentIDLink = String.Empty; ;
            parentIDLink = (from a in DB.PersonalFinanceDB.FinaceType where a.TypeID == model.ParentTypeID select a.IDLink).Single();
          
            model.TypeID = typeID;
            model.IDLink = parentIDLink + "-" + typeID.ToString();
            model.ParentTypeID = model.ParentTypeID;
            DB.PersonalFinanceDB.FinaceType.InsertOnSubmit(model);
            DB.PersonalFinanceDB.SubmitChanges();
        }

        public void DelFinaceType(int typeID)
        {
            FinaceType model = DB.PersonalFinanceDB.FinaceType.Single(c => c.TypeID == typeID);

            DB.PersonalFinanceDB.FinaceType.DeleteOnSubmit(model);

            DB.PersonalFinanceDB.SubmitChanges();
        }

        public void UpdateFinaceType(FinaceType model)
        {
            FinaceType finaceType = DB.PersonalFinanceDB.FinaceType.Single(a => a.TypeID == model.TypeID);

            finaceType.TypeName = model.TypeName;
            finaceType.Remark = model.Remark;

            DB.PersonalFinanceDB.SubmitChanges();
        }

        /// <summary>
        /// 是否该支付类型下,已经有财务信息了,还要判断有没有子类型,有子类型,也不能删除\或判断子类型,有没有财务信息
        /// </summary>
        /// <param name="typeID"></param>
        public bool IsCanDel(int typeID)
        {
            //财务记录数
            int finaceRowCount = (from a in DB.PersonalFinanceDB.FinaceType
                            where a.TypeID == typeID
                            join b in DB.PersonalFinanceDB.FinaceRecord on a.TypeID equals b.FinaceTypeID
                            select b.FinaceTypeID).Count();


            //子节点数目
            int childNodeRowCount = DB.PersonalFinanceDB.FinaceType.Count(a => a.ParentTypeID == typeID);

            return finaceRowCount == 0 && childNodeRowCount == 0;
        }



    }
}
